package dao.impl;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import util.JDBCUtil;
import util.ModelConvertUtil;
import dao.IStudentDao;
import entity.Student;

public class StudentDaoImpl implements IStudentDao{

	@Override
	public List<Student> selectAll() {
		ArrayList<Student> list = new ArrayList<Student>();
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT student_id,student_name,student_age,gender,class_id FROM student";
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				int id = resultSet.getInt("student_id");
				String name = resultSet.getString("student_name");
				int age = resultSet.getInt("student_age");
				String gender = resultSet.getString("gender");
				int class_id = resultSet.getInt("class_id");
				Student student = new Student(id, name, age, gender,class_id);
				list.add(student);
			}
			
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		
		return list;
	}

	@Override
	public List<Student> search(String searchName) {
		ArrayList<Student> list = new ArrayList<Student>();
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT * FROM student WHERE student_name like ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, "%" + searchName + "%");
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("student_id");
				String name = resultSet.getString("student_name");
				int age = resultSet.getInt("student_age");
				String gender = resultSet.getString("gender");
				int class_id = resultSet.getInt("class_id");
				Student student = new Student(id, name,age,gender,class_id);
				list.add(student);
			}
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	}

	@Override
	public Integer insert(Student student) {
		int count = 0;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO student (student_name,student_age,gender,class_id) VALUES (?,?,?,?)";
			preparedStatement = connection.prepareStatement(sql);	
			preparedStatement.setString(1, student.getStudent_name());
			preparedStatement.setInt(2, student.getStudent_age());
			preparedStatement.setString(3, student.getGender());
			preparedStatement.setInt(4, student.getClass_id());
			count = preparedStatement.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		return count;
	}

	@Override
	public Integer update(Student student) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "UPDATE student SET student_name = ?,student_age = ?,gender = ?,class_id = ? WHERE student_id = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1,student.getStudent_name());
			preparedStatement.setInt(2, student.getStudent_age());
			preparedStatement.setString(3, student.getGender());
			preparedStatement.setInt(4, student.getClass_id());
			preparedStatement.setInt(5, student.getStudent_id());
			count = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		return count;
	}

	@Override
	public Integer deleteById(Integer id) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "delete from student where student_id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("删除成功");
			} else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		return count;
	}

	@Override
	public Student selectById(int id) {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		Student student = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT student_id,student_name,student_age,gender,class_id FROM student where student_id=?";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, id);
			resultSet = statement.executeQuery();
			
			if (resultSet.next()) {
				int studentId = resultSet.getInt("student_id");
				String name = resultSet.getString("student_name");
				int age = resultSet.getInt("student_age");
				String gender = resultSet.getString("gender");
				int class_id = resultSet.getInt("class_id");
				student = new Student(studentId, name, age, gender,class_id);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		
		return student;

	}

	@Override
	public Integer getTotalCount() {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select count(*) from student";
			statement = connection.prepareStatement(sql);
			resultSet = statement.executeQuery();
			
			if (resultSet.next()) {
				count = resultSet.getInt(1); // 第一列
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		
		return count;

	}

	@Override
	public List<Student> pageList(int offset, Integer pageSize) {
		ArrayList<Student> list = new ArrayList<Student>();
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT student_id,student_name,student_age,gender,class_id FROM student limit ?,?";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, offset);
			statement.setInt(2, pageSize);
			System.out.println(statement);
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("student_id");
				String name = resultSet.getString("student_name");
				int age = resultSet.getInt("student_age");
				String gender = resultSet.getString("gender");
				int class_id = resultSet.getInt("class_id");
				Student student = new Student(id, name, age, gender,class_id);
				list.add(student);
			}
			
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		
		return list;

	}

	@Override
	public List<Student> selectByName(String name) {
		ArrayList<Student> list = new ArrayList<Student>();
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT student_id,student_name,student_age,gender,class_id FROM student where student_name=? ";
			statement = connection.prepareStatement(sql);
			statement.setString(1, name);
			System.out.println(statement);
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("student_id");
				String student_name = resultSet.getString("student_name");
				int age = resultSet.getInt("student_age");
				String gender = resultSet.getString("gender");
				int class_id = resultSet.getInt("class_id");
				Student student = new Student(id, student_name, age, gender,class_id);
				list.add(student);
			}
			
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		
		return list;
	}

	@Override
	public List<Map<String, Object>> selectStudentBanjiInfo() {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		List<Map<String, Object>> list = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT s.student_id,s.student_name,s.student_age,s.gender,b.name AS banjiName FROM student AS s INNER JOIN class AS b ON s.class_id=b.id;";
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			list = ModelConvertUtil.converToList(resultSet);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		return list;
	}

}

